<?php

namespace App\Http\Controllers\Company\English;

use App\Facades\Style;
use App\Http\Controllers\Company\CommonController;
use App\Models\Common\CollArea;
use App\Models\Common\CollOption;
use App\Models\Common\English\EnCollArea;
use App\Models\Common\English\EnCollOption;
use App\Models\Company\ResumeFavorite;
use App\Models\Personal\BrowseJob;
use App\Models\Personal\ResumeSend;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class BrowseJobController extends CommonController
{

    /**
     * 谁浏览过我的职位
     * @param Request $request
     * @return mixed
     */
    public function index(Request $request){
        $user = auth()->guard('company')->user();
        //获取面试通知记录
        $relist = DB::table('browse_job as log')
            ->join('resume as t1', 'log.resume_id', '=', 't1.id')
            ->join('company_job as t2', 'log.job_id', '=', 't2.id')
            ->select(
                'log.id','log.updated_at','t1.name','t1.sex','t1.birthday',
                't1.education','t1.homeaddress','t1.language','t2.job_title','t2.lang_type','t1.intentionjobs','log.resume_id','log.job_id'
            )
            ->where('log.company_uid',$user->uid)
            ->where(function ($query) use ($request) {
                if(strlen($request->keyWord)>0){
                    $query->where('t2.job_title', 'like', '%'.$request->keyWord.'%')->orWhere('t1.name','like','%'.$request->keyWord.'%');
                }
            })
            ->orderBy('log.updated_at','DESC')
            ->paginate(20);
        //print_r($queries );
        //dd($relist);

        foreach($relist as $v) {

            if ($v->lang_type == 'en') {
                switch ($v->sex) {
                    case "1":
                        $v->cnsex = 'Man';
                        break;
                    case "2":
                        $v->cnsex = 'Woman';
                        break;
                    default:
                        $v->cnsex = '';
                }
                $v->education = EnCollOption::getOptionName($v->education, 'education');
                $v->homeaddress = EnCollArea::getAreaName($v->homeaddress);
                $v->birthday = birthday($v->birthday);
            }else{
                switch ($v->sex) {
                    case "1":
                        $v->cnsex = '男';
                        break;
                    case "2":
                        $v->cnsex = '女';
                        break;
                    default:
                        $v->cnsex = '';
                }
                $v->education = CollOption::getOptionName($v->education, 'education');
                $v->homeaddress = CollArea::getAreaName($v->homeaddress);
                $v->birthday = birthday($v->birthday);
            }
        }
        return Style::view('company.english.browseJobIndex',compact('relist'));
    }

    /**
     * 放入人才库
     * @param Request $request
     */
    public function toFavorite(Request $request)
    {
        $user = auth()->guard('company')->user();
        if(is_array($request->selected_id)){
            $browseJob=BrowseJob::select('resume_id','job_id')->where('company_uid',$user->uid)->whereIn('id',$request->selected_id)->get();
            foreach($browseJob as $v){
               $favorite=ResumeFavorite::select('id')->where('uid',$user->uid)->where('status','!=',4)->where("resume_id",$v->resume_id)->first();
               if(!empty($favorite->id)){
                   $favorite->updated_at=date('Y-m-d H:i:s');
                   $favorite->status = 0;
                   $favorite->save();
               }else{
                   $favorite = new ResumeFavorite;
                   $favorite->uid = $user->uid;
                   $favorite->resume_id = $v->resume_id;
                   $favorite->job_id = $v->job_id;
                   $favorite->status = 0;
                   $favorite->save();
               }
            }
            return back()->withInput()->with('info',"Resumes have been added to the talent pool!");
        }
    }
}